<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/6/23
 * Time: 15:54
 */

namespace app\adminapiv3\service;


class IpsOriginalRule extends Base
{
    function list($hostId, $params, $limit = 10)
    {
        //1.检查系统
        $hostServer = new \app\common\service\Host();
        $hostInfo = $hostServer->info($hostId);

        if ($hostInfo['os'] == 2) {
            $this->setError($this->CODE_FAIL, 'windows无规则');
            return false;
        }

        //2. 检查是否开启IPS模式
        $ipsConfigServer = new \app\common\service\IpsConfig();
        $info = $ipsConfigServer->findData(['host_id' => $hostId]);

        if(empty($info) || ($info['type'] != 1 && $info['status'] == 1)){
            $this->setError($this->CODE_FAIL,'未开启IPS设置');
            return false;
        }

        $server = new \app\common\service\IpsOriginalRule();
        $list = $server->list($hostId, $params, $limit);

        return $list;
    }

    /**
     * @name 修改
     * @param $id
     * @param $update
     * @author wx
     * @date 2019/11/21 10:51
     */
    function modify($id, $update)
    {
        $this->checkOptionHost($update['host_id']);

        $IpsOriginalRule = new \app\common\service\IpsOriginalRule();
        $return = $IpsOriginalRule->checkRequestData($update['host_id'], $id);
        $IpsOriginalRule->modify(['id' => $id], $update);

        return $return;
    }

    /**
     * @name 删除
     * @param $id
     * @author wx
     * @date 2019/11/21 10:56
     */
    function del($id, $hostId)
    {
        $this->checkOptionHost($hostId);

        $IpsOriginalRule = new \app\common\service\IpsOriginalRule();
        $return = $IpsOriginalRule->checkRequestData($hostId, $id);
        $IpsOriginalRule->del(['id' => $id, 'host_id' => $hostId]);

        return $return;
    }

    /**
     * @name 转换规则并上传
     * @param $hostId
     * @return bool
     * @author wx
     * @date 2020/6/24 8:51
     */
    function importRule($hostId)
    {
        $this->checkOptionHost($hostId);

        //1.检查系统
        $hostServer = new \app\common\service\Host();
        $hostInfo = $hostServer->info($hostId);
        if ($hostInfo['os'] == 2) {
            $this->setError($this->CODE_FAIL, 'windows无规则');
            return false;
        }

        //2. 检查是否开启IPS模式
        $ipsConfigServer = new \app\common\service\IpsConfig();
        $info = $ipsConfigServer->findData(['host_id' => $hostId]);

        if(empty($info) || ($info['firewall'] == 1)){
            $this->setError($this->CODE_FAIL,'已锁定防火墙，此操作无效!');
            return false;
        }

        //清空防火墙规则
        $server = new \app\common\service\IpsOriginalRule();
        $server->clear(['host_id' => $hostId]);

        return true;
    }

}